package com.ruoyi.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.GetRequestDataUtils;
import com.ruoyi.system.service.INotifyService;
import com.ruoyi.system.service.ISysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TreeMap;

/**
 * 回调Controller
 *
 * @author Ericcc
 * @date 2021-02-12
 */
@Controller
@RequestMapping("/notify")
public class NotifyController extends BaseController {

    @Autowired
    private INotifyService notifyService;

    @Autowired
    private ISysOrderService sysOrderService;

    private static int reqsize = 0;


    @PostMapping("/razorpay")
    @ResponseBody
    public String razorpay(HttpServletRequest request, HttpServletResponse response)
    {
        //Razorpay回调验证(上一套没做)
//        String sign = request.getHeader("X-Razorpay-Signature");

        JSONObject requestJsonObject = GetRequestDataUtils.getRequestJsonObject(request);
        logger.debug("razorpay代付回调的数据 == {}", requestJsonObject.toString());
        notifyService.razorpay(requestJsonObject);
        reqsize++;
        System.out.println("成功请求数"+reqsize);
        return "250";
    }

    @PostMapping("/cashfreeResult")
    @ResponseBody
    public String cashfreePay(HttpServletRequest request)
    {
        //Razorpay回调验证(上一套没做)
//        String sign = request.getHeader("X-Razorpay-Signature");
        TreeMap<String, String> params = GetRequestDataUtils.getParams(request);
        logger.debug("cashfree聚合支付回调数据 == {}", params.toString());
        notifyService.cashfreeResult(params);
        return "250";
    }

//    @PostMapping("/India/testOrder")
//    @ResponseBody
//    @Log(title = "测试订单", businessType = BusinessType.PAYREQ)
//    public AjaxResult testOrder(SysOrder sysOrder)
//    {
//        return toAjax(sysOrderService.insertSysOrder(sysOrder));
//    }

}
